<template>
  <div class="my-input-valueber">
    <!-- 递减 -->
    <span
      @click="sub"
      role="button"
      class="my-input-valueber__decrease"
      :class="{ 'is-disabled': value == 0 }"
    >
      -
    </span>
    <!-- 累加 -->
    <span
      @click="add"
      role="button"
      class="my-input-valueber__increase"
      :class="{ 'is-disabled': value == 10 }"
    >
      +
    </span>
    <div class="my-input">
      <!-- 数字显示区域 -->
      <span class="my-input__inner">{{ value }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BaseInputNum',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
    };
  },
  methods: {
    // +号的点击事件
    add() {
      // 小于10才允许++
      if (this.value < 10) {
        // 下面这句话没用了，此时value是props传过来的数据
        // 如果直接改了会破坏单向数据流
        // this.value++;
        // 通知父组件去+和-
        this.$emit('input', this.value + 1)
      }
    },

    // -号的点击事件
    sub() {
      // 大于0才允许--
      if (this.value > 0) {
        // this.value--;
        this.$emit('input', this.value - 1)
      }
    },
  },

};
</script>

<style>
.my-input-valueber {
  position: relative;
  display: inline-block;
  width: 180px;
  line-height: 38px;
}
.my-input-valueber span {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}
.my-input-valueber .my-input {
  display: block;
  position: relative;
  font-size: 14px;
  width: 100%;
}
.my-input-valueber .my-input__inner {
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  height: 40px;
  line-height: 40px;
  outline: none;
  padding: 0 15px;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
  padding-left: 50px;
  padding-right: 50px;
  text-align: center;
}
.my-input-valueber .my-input-valueber__decrease,
.my-input-valueber .my-input-valueber__increase {
  position: absolute;
  z-index: 1;
  top: 1px;
  width: 40px;
  height: auto;
  text-align: center;
  background: #f5f7fa;
  color: #606266;
  cursor: pointer;
  font-size: 13px;
}
.my-input-valueber .my-input-valueber__decrease {
  left: 1px;
  border-radius: 4px 0 0 4px;
  border-right: 1px solid #dcdfe6;
}
.my-input-valueber .my-input-valueber__increase {
  right: 1px;
  border-radius: 0 4px 4px 0;
  border-left: 1px solid #dcdfe6;
}
.my-input-valueber .my-input-valueber__decrease.is-disabled,
.my-input-valueber .my-input-valueber__increase.is-disabled {
  color: #c0c4cc;
  cursor: not-allowed;
}
</style>
